#------------------------------------------------------------------------------#
######################## Descriptive - Figures ############################
#------------------------------------------------------------------------------#

rm(list = ls())

# Setting directory: adjust accordingly to 

setwd()

#------------------------------------------------------------------------------#
# Loading and installing necessary packages ##################################
#------------------------------------------------------------------------------#

# Installing required packages 

packages <- c('tidyverse', 'fixest', 'sjPlot', 'modelsummary', 
              'performance', 'car', 'psych', 'REdaS')

# Checking if is installed (and install if not)

packages.check <- lapply(
  packages,
  FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
      install.packages(x, dependencies = TRUE)
      library(x, character.only = TRUE)
    }
  }
)

rm(packages, packages.check)

#------------------------------------------------------------------------------#
# Loading data base  #########################################################
#------------------------------------------------------------------------------#

Base <- readRDS(file = "data/Base.rds")

#------------------------------------------------------------------------------#
# Renaming Variables  ########################################################
#------------------------------------------------------------------------------#

Base <- Base %>%
  mutate(
    bf = `Belief climate change`,
    bf2 = `Belief climate change (binary)`,
    cau = `Causes of climate change`,
    imp = `Impacts of climate change`,
    pi_lf = `Political Ideology (Left-Right)`,
    pi_cs = `Political Ideology (Conservatism-Progressive)`, 
    sk = `Subjective knowledge`,
    ok = `Objective knowledge`,
    sc = `Scientific consensus`,
    ts = `Trust in scientists`, 
    nep = `The New Ecological Paradigm (NEP)`,
    ii = `Individualism index`,
    ei = `Egalitarianism index`,
    pe = `Personal Experience (extreme weather events)`)

#------------------------------------------------------------------------------#
# 1. Dependent Variables  ####################################################
#------------------------------------------------------------------------------#

# Country labels

countries <- c(
  "2" = "Argentina",
  "1" = "Brazil",
  "3" = "Chile",
  "4" = "Colombia",
  "5" = "Ecuador",
  "6" = "Mexico",
  "7" = "Peru")

order <- c(
  "2",
  "1",
  "3",
  "4",
  "5",
  "6",
  "7")

# Existence of Climate Change
Base %>%
  mutate(Pais = as.factor(Pais)) %>%
  ggplot(aes(bf)) +
    geom_bar() +
    labs(x = "Existence of Climate Change",
        y = "Observations") +
    theme_bw() +
    theme(axis.text.y = element_text(color="black", size = 9),
        axis.text.x = element_text(color="black"),
        axis.title.y = element_text(size = 11, margin = margin(t = 0, r = 8, b = 0, l = 0)),
        axis.title.x = element_text(size = 11, margin = margin(t = 8, r = 0, b = 0, l = 0)),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
    facet_wrap(. ~ factor(Pais, levels = order, labels = countries)) #8.5 x 6

# Anthropogenic Causes of Climate Change
Base %>%
  drop_na(cau) %>%
  mutate(Pais = as.factor(Pais)) %>%
  ggplot(aes(as.factor(cau))) +
  geom_bar() +
  labs(x = "Anthropogenic Causes of Climate Change",
       y = "Observations") +
  theme_bw() +
  theme(axis.text.y = element_text(color="black", size = 9),
        axis.text.x = element_text(color="black"),
        axis.title.y = element_text(size = 11, margin = margin(t = 0, r = 8, b = 0, l = 0)),
        axis.title.x = element_text(size = 11, margin = margin(t = 8, r = 0, b = 0, l = 0)),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  facet_wrap(. ~ factor(Pais, levels = order, labels = countries)) 

# Impacts of Climate Change
Base %>%
  drop_na(imp) %>%
  mutate(Pais = as.factor(Pais)) %>%
  ggplot(aes(as.factor(imp))) +
  geom_bar() +
  labs(x = "Consequences of Climate Change",
       y = "Observations") +
  theme_bw() +
  theme(axis.text.y = element_text(color="black", size = 9),
        axis.text.x = element_text(color="black"),
        axis.title.y = element_text(size = 11, margin = margin(t = 0, r = 8, b = 0, l = 0)),
        axis.title.x = element_text(size = 11, margin = margin(t = 8, r = 0, b = 0, l = 0)),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  facet_wrap(. ~ factor(Pais, levels = order, labels = countries)) 

# Belief in Climate Change Index
Base %>%
  mutate(index_score = round(index_score, 2)) %>%
  drop_na(index_score) %>%
  mutate(Pais = as.factor(Pais)) %>%
  ggplot(aes(as.factor(index_score))) +
  geom_bar() +
  labs(x = "Belief in Climate Change Index Scores",
       y = "Observations") +
  theme_bw() +
  theme(axis.text.y = element_text(color="black", size = 9),
        axis.text.x = element_text(color="black"),
        axis.title.y = element_text(size = 11, margin = margin(t = 0, r = 8, b = 0, l = 0)),
        axis.title.x = element_text(size = 11, margin = margin(t = 8, r = 0, b = 0, l = 0)),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  facet_wrap(. ~ factor(Pais, levels = order, labels = countries)) 

# Correlation Table
dependent <- Base %>%
  dplyr::select(bf, cau, imp, index_score) %>%
  drop_na()

tab_corr(dependent)

datasummary_correlation(dependent,
                        output = "latex_tabular",
                        method = "pearson")

# Alpha
Base %>%
  dplyr::select(bf2, cau, imp) %>%
  alpha()

#------------------------------------------------------------------------------#
# 2. Independent Variables  ####################################################
#------------------------------------------------------------------------------#

# Correlation Table of Pyschological variables
independent_psycho <- Base %>%
  dplyr::select(sk, ok, sc, ts, nep, pe) %>%
  drop_na()

tab_corr(independent_psycho) 

datasummary_correlation(independent_psycho,
                        output = "latex_tabular",
                        method = "pearson")

